package com.xy.work.template.base.config;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.mybatis.spring.annotation.MapperScan;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author niuniu
 * @Date: 2021/5/21 13:58
 * @Description: App应用相关操作
 */
@Configuration
@MapperScan("com.xy.work.template.**.mapper")
@Data
public class AppConfig {
    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private String redisPort;
    @Value("${spring.redis.password}")
    private String redisPwd;
    /**
     * 配置分布式锁的redisson
     * @return
     */
    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        //单机方式
        if(StrUtil.isEmpty(redisPwd)){
            config.useSingleServer().setAddress("redis://"+redisHost+":"+redisPort);
        }else {
            config.useSingleServer().setPassword(redisPwd).setAddress("redis://"+redisHost+":"+redisPort);
        }
        //集群
        //config.useClusterServers().addNodeAddress("redis://192.31.21.1:6379","redis://192.31.21.2:6379")
        return Redisson.create(config);
    }
//    /**
//     * feign调用丢失token解决方式，新增拦截器
//     * @return
//     */
//    @Bean
//    public RequestInterceptor requestInterceptor(){
//        return template -> {
//            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            if(ObjectUtil.isNotEmpty(attributes)){
//                HttpServletRequest httpServletRequest = attributes.getRequest();
//                if(ObjectUtil.isNotEmpty(httpServletRequest)){
//                    String token = httpServletRequest.getHeader("token");
//                    template.header("token",token);
//                }
//            }
//        };
//
//    }




}
